package service

import (
	"e_mall_server/dao/redis"
	"fmt"
	"github.com/mojocn/base64Captcha"
	"image/color"
)

/**
 * @Author: zhaobin
 * @Description TODO
 * @Date: 2022-04-25 15:57
 */

// 设置自带的store，底层将验证码信息写入到redis
//var store = base64Captcha.DefaultMemStore
// 配置RedisStore  RedisStore实现base64Captcha.Store接口，底层将验证码信息写入到redis
var store base64Captcha.Store = redis.RedisStore{}

// MakeCaptcha 生成验证码
func MakeCaptcha() (string, string, error) {
	var driver base64Captcha.Driver
	// 配置验证码信息
	driverString := base64Captcha.DriverString{
		Height:          40,
		Width:           100,
		NoiseCount:      0,
		ShowLineOptions: 2 | 4,
		Length:          4,
		Source:          "1234567890qwertyuioplkjhgfdsazxcvbnm",
		BgColor: &color.RGBA{
			R: 3,
			G: 102,
			B: 214,
			A: 125,
		},
		Fonts: []string{"wqy-microhei.ttc"},
	}
	//ConvertFonts 按名称加载字体
	driver = driverString.ConvertFonts()
	//创建 Captcha
	captcha := base64Captcha.NewCaptcha(driver, store)
	//Generate 生成随机 id、base64 图像字符串
	id, b64s, err := captcha.Generate()
	return id, b64s, err

}

// VerifyCaptcha 验证验证码是否争取
func VerifyCaptcha(id string, VerifyValue string) bool {
	fmt.Println(id, VerifyValue)
	// true表示清除验证码
	if store.Verify(id, VerifyValue, true) {
		return true
	} else {
		return false
	}
}
